package com.fjb.controller.search;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonPagesResult;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.entity.PageQuery;
import com.fjb.common.enums.product.ProductStatus;
import com.fjb.controller.base.BaseController;
import com.fjb.pojo.product.query.ProductInfoQuery;
import com.fjb.pojo.product.vo.ProductInfoPageVo;
import com.fjb.service.product.ProductInfoService;

/**
 * @Description:搜索
 * @author hemiao
 * @time:2020年1月28日 下午7:48:07
 */
@Controller
@RequestMapping("/search")
public class SearchController extends BaseController{
		
	@Autowired
	private ProductInfoService productInfoService;
	
	/**
	 * @Description:搜索商品列表
	 * @param request
	 * @return
	 * JsonPagesResult<List<ProductInfoPageVo>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月16日 下午7:40:44
	 */
	@RequestMapping(value="/selectProductList")
	@ResponseBody
	public JsonPagesResult<List<ProductInfoPageVo>> selectProductList(HttpServletRequest request,Integer searchType,
			PageQuery pageQuery,Integer brandId ,Integer categoryId,String sort,String sortType,Integer dealType){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		// 搜索的关键字
		String keyword = request.getParameter("keyword");
		if(StringUtils.isBlank(keyword)) {
			return new JsonPagesResult<List<ProductInfoPageVo>>(null, httpCode, null,0);
		}
		if(keyword.equals("undefined")) {
			keyword = "";
		}
		ProductInfoQuery infoQuery = new ProductInfoQuery();	
		// 品牌id
		if(brandId!=null) {
			if(!brandId.equals(0)) {	
				infoQuery.setBrandId(brandId);
			}
		}
		// 分类id 
		if(categoryId!=null) {
			if(!categoryId.equals(0)) {				
				infoQuery.setCategoryId(categoryId);
			}
		}
		// 交易类型
		if(dealType!=null) {
			if(!dealType.equals(0)) {					
				infoQuery.setDealType(dealType);
			}
		}
		String sortStr = "create_time";
		if(StringUtils.isNotBlank(sort)) {
			if(sort.equals("current_price")) {
				sortStr = sort;
			}
		}
		String sortTypeStr = "DESC";
		if(StringUtils.isNotBlank(sortType)) {
			if(sortType.equals("asc")) {
				sortTypeStr = sortType;
			}
		}
		String province = request.getParameter("province");
		if(StringUtils.isNotBlank(province)) {
			if(!province.equals("省份")) {				
				infoQuery.setProvince(province);
			}
		}
		String city = request.getParameter("city");
		if(StringUtils.isNotBlank(city)) {
			if(!city.equals("城市")) {	
				infoQuery.setCity(city);
			}
		}		
		String district = request.getParameter("district");
		if(StringUtils.isNotBlank(district)) {
			if(!district.equals("区县")) {	
				infoQuery.setDistrict(district);
			}
		}
		
		infoQuery.setKeyword(keyword);
		infoQuery.setSortStr(sortStr);
		infoQuery.setSortTypeStr(sortTypeStr);		
		infoQuery.setProductStatus(ProductStatus.STATUS_1.getCode());
		return productInfoService.selectSearchPageList(infoQuery, pageQuery);
	}
}
